package visibility;

public class VisibilityDemo {
    private long count = 0L;

    // 不加synchronized导致count不安全
    public  void add10M() {
        for(int i=0; i<1000_000; i++) {
            count += 1L;
        }
    }

    public static long calc() throws Exception {
        VisibilityDemo demo = new VisibilityDemo();
        Thread t1 = new Thread(
                () -> demo.add10M()
        );
        Thread t2 = new Thread(
                () -> demo.add10M()
        );
        // 启动线程
        t1.start();
        t2.start();
        // 等待两个线程执行结束
        t1.join();
        t2.join();
        return demo.count;
    }

    public static void main(String[] args) throws Exception{
        System.out.println(calc());
    }
}
